<?php
namespace Sys\Controller;
use Think\Controller;
class LoginController extends Controller{

    public function __construct()  {
        parent::__construct();
    }
    public function login(){
        $this->display();
    }
    public function doLogin(){
       // echo I('post.role');exit;
        if(I('post.role')=='0'){
            $this->teacherLogin();
        }else{
            $this->studentLogin();
        }
    }

   public function teacherLogin(){
        $where = [
            'mtel' => I("post.mtel"),
            'pwd' => I("post.pwd"),
        ];
        $member = M('teachers')
            ->join('__ROLES__ ON __TEACHERS__.role = __ROLES__.role_id')
            ->where($where)
            ->find();
      // dump($member['menu_ids']);
        if ($member == false) {
            $this->error('登录失败', U('login'));
        } else {
            $arr = explode(',', $member['menu_ids']);
            session('name', $member['tname']);
            session('role', $arr);
            $this->success("登陆成功", U('Menus/index'));
        }

    }
    public function studentLogin(){
        $where = [
            'mtel' => I("post.mtel"),
            'pwd' => I("post.pwd"),
        ];
        $m = M('students');
        $member = $m->where($where)
            ->join('__ROLES__ ON __STUDENTS__.role = __ROLES__.role_id')->where($where)->find();
        if ($member == false) {
            $this->error('登录失败', U('login'));
        } else {
            $arr = explode(',', $member['menu_ids']);
            session('name', $member['sname']);
            session('role', $arr);
            $this->success("登陆成功", U('Menus/index'));
        }
    }


    public function doLogout(){
        session(null);
        $this->success('再见', U('login'));
    }
}